package de.tum.in.eist.poll.client.gui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import de.tum.in.eist.poll.client.gui.LectureCreatePanel;

import de.tum.in.eist.poll.client.control.EIST_POLL;
import de.tum.in.eist.poll.client.control.HeadlineHandler;
import de.tum.in.eist.poll.client.control.LectureCreateHandler;
import de.tum.in.eist.poll.shared.Lecture;
import de.tum.in.eist.poll.shared.service.NotificationsPushService;
import de.tum.in.eist.poll.shared.service.NotificationsPushServiceAsync;

public class Dashboard extends VerticalPanel {

	private EIST_POLL eistPoll;
	private Headline headline;
	private LectureCreatePanel createLecture;
	private LectureWidgetList widgetList;
	private DateSelectorPanel dateSelector;
	private OverAllChartWidget overAllChart;

	final NotificationsPushServiceAsync pollSvc = GWT
			.create(NotificationsPushService.class);

	public Dashboard(EIST_POLL eistPoll) {
		this.eistPoll = eistPoll;
		init();
		addContent();
		listenToNotifications();
	}

	private void listenToNotifications() {
		AsyncCallback<Lecture> callback = new AsyncCallback<Lecture>() {

			@Override
			public void onSuccess(Lecture result) {
				if (result != null) {
					eistPoll.getUser().getNotifications().add(result);
					headline.getNotifyButton().setText(
							String.valueOf(eistPoll.getUser()
									.getNotifications().size()));
					System.out.println("received notification: " + result);
				}
				pollSvc.requestNotification(eistPoll.getUser(), this);
			}

			@Override
			public void onFailure(Throwable caught) {
				System.out.println("received notification error: " + caught);
				pollSvc.requestNotification(eistPoll.getUser(), this);
			}
		};

		pollSvc.requestNotification(eistPoll.getUser(), callback);
	}

	private void init() {
		// Headline
		headline = new Headline(eistPoll.getUser());
		new HeadlineHandler(this, eistPoll, headline);
		// Create lecture panel
		createLecture = new LectureCreatePanel();
		new LectureCreateHandler(this, eistPoll.getUser(), createLecture);
		// Over all statistic
		overAllChart = new OverAllChartWidget();
		overAllChart.setVisible(false);
		// Widget List
		widgetList = new LectureWidgetList(eistPoll.getUser(), headline, this);

		// Add styles
		addStyleName("dashbordForm");
	}

	private void addContent() {
		add(headline);
		if (eistPoll.getUser().isLecturer()) {
			add(createLecture);
		}
		add(overAllChart);
		add(widgetList);
	}

	/**
	 * Update the over all statistic with the lectures from the current view
	 */
	public void updateOverAllChart() {
		int[] result = widgetList.getOverAllStats();
		int[] stats = new int[LectureVotePanel.VOTES];
		
		for (int i = 0; i < LectureVotePanel.VOTES; i++) {
			stats[i] = result[i];
		}
		
		String title = "Over all statistic";		
		switch(result[LectureVotePanel.VOTES]) {
		case LectureWidgetList.CURRENT_WEEK_VIEW:
			title += " - this week";
			break;
		case LectureWidgetList.LAST_WEEK_VIEW:
			title += " - last week";
			break;
		case LectureWidgetList.SEMESTER_VIEW:
			title += " - semester";
			break;
		default: break;
		}
		overAllChart.update(stats, title);
	}

	public boolean isCreateLectureVisible() {
		if (createLecture == null)
			return false;
		return createLecture.isVisible();
	}

	public boolean isDateSelectorVisible() {
		if (dateSelector == null)
			return false;
		return dateSelector.isVisible();
	}

	public boolean isOverAllChartVisible() {
		if (overAllChart == null)
			return false;
		return overAllChart.isVisible();
	}

	public LectureWidgetList getWidgetList() {
		return widgetList;
	}

	public LectureCreatePanel getCreateLecture() {
		return createLecture;
	}

	public DateSelectorPanel getDateSelector() {
		return dateSelector;
	}

	public Widget getOverAllChart() {
		return overAllChart;
	}
}
